package com.jinlejia.erp.ui.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.WorkGuideBean;
import com.jinlejia.erp.databinding.FragmentWorksBinding;
import com.jinlejia.erp.utils.CallUtils;
import com.jinlejia.erp.widget.TagCloudView;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import java.util.ArrayList;

public class WorksFragment extends Fragment {

    View view;
    private SlimAdapter slimAdapter;

    public WorksFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private FragmentWorksBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_works, container, false);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }

    private void initView() {
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        slimAdapter = SlimAdapter.create()
                //标题信息
                .register(R.layout.item_works_guide, new SlimInjector<WorkGuideBean>() {
                    @Override
                    public void onInject(final WorkGuideBean data, IViewInjector injector) {
                        injector.text(R.id.dept, data.getSupDeptName())
                                .text(R.id.content, data.getWorkContent())
                                .with(R.id.tag, new IViewInjector.Action() {
                                    @Override
                                    public void action(View view) {
                                        if (!TextUtils.isEmpty(data.getExeNumber())) {
                                            data.getUserNameList().add(0,"座机(优先)");
                                            data.getUserTelList().add(0, data.getExeNumber().replace("-",","));
                                        }
                                        ((TagCloudView) view).setTags(data.getUserNameList());
                                        ((TagCloudView) view).setOnTagClickListener(new TagCloudView.OnTagClickListener() {
                                            @Override
                                            public void onTagClick(int position) {
                                                CallUtils.CallTel(getActivity(), data.getUserTelList().get(position));
                                            }
                                        });
                                    }
                                })
                        .with(R.id.tag_layout, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                if( data.getUserNameList().isEmpty() ) {
                                    view.setVisibility(View.GONE);
                                }
                            }
                        });
                    }
                }).attachTo(binding.recyclerView);


    }


    public void setData(ArrayList<WorkGuideBean> data) {
        if (null != slimAdapter)
            slimAdapter.updateData(data);
    }

}
